// 对数据进行中间处理，如埋点
import {
  UPDATE_MSG, 
  UPDATE_MISIC_LIST, 
  UPDATE_ROLE, 
  UPDATE_TOKEN,
  UPDATE_ARTICLE_LIST,
  UPDATE_ARTICLE_CATES,
  ADD_ARTICLE,
  DEL_ARTICLE,
  ARTICLE_INFO
} from '../reducersTypes'
import {fetchMusicList} from '@/api'
import {fetchArticleCates, fetchArticleList,fetchAddArticle, fetchDelArticle,fetchArticleInfo} from '@/api/fetchArticle'
import {
  fetchUserLogin, 
  fetchUserInfo,
} from '@/api/fetchUser'
const handleMsg = payload => ({type: UPDATE_MSG, payload})

const handleMusic = payload => {
  return dispatch => {
    return fetchMusicList(payload).then(res => {
      dispatch({type: UPDATE_MISIC_LIST, payload: res.song.list})
    })
  }
}

//用户
const handleToken = payload => {
  return dispatch => {
    payload.key = 1;
    fetchUserLogin(payload).then(res => {
      dispatch({type: UPDATE_TOKEN, payload: res.data})
    })
  }
}
const handleRole = () => {
  return dispatch => {
    fetchUserInfo().then(res => {
      console.log(res.data);
      dispatch({type: UPDATE_ROLE, payload: res.data})
    })
  }
}
//文章
const handleArticleList = data => {
  return dispatch => {
    fetchArticleList(data).then(res => {
      dispatch({type: UPDATE_ARTICLE_LIST, payload: res.data})
    })
  }
}
//类别
const handleArticleCates = () => {
  return dispatch => {
    fetchArticleCates().then(res => {
      // console.log(res);
      dispatch({type: UPDATE_ARTICLE_CATES, payload: res.data})
    })
  }
}
//添加
const handleAddArticle = ({data, back}) => {
  return dispatch => {
    fetchAddArticle(data).then(res => {
      dispatch({type: ADD_ARTICLE, payload: res.data})
      back()
    })
  }
}
//删除
const handleDelArticle = id => {
  return dispatch => {
    return fetchDelArticle({id}).then((res) => {
      console.log(res);
      dispatch({type: DEL_ARTICLE, payload: id})
    })
  }
}
//编辑信息
const handleArticleInfo = id => {
  return dispatch => {
    return fetchArticleInfo({id}).then(res => {
      // console.log(res);
      dispatch({type: ARTICLE_INFO, payload: res.data})
    })
  }
}

export {
  handleMsg,
  handleMusic,
  handleToken,
  handleRole,
  handleArticleList,
  handleArticleCates,
  handleAddArticle,
  handleDelArticle,
  handleArticleInfo
}
